package top.truism.sword;

/**
 * 删除链表中的重复节点
 */
public class RemoveDuplicateNode {

    public ListNode removeDuplicateNode(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode origin = new ListNode(-1);
        origin.next = head;
        ListNode pre = origin;
        ListNode next = pre.next;
        while (next != null) {
            if(next.next != null && next.value == next.next.value) {
                while (next.next != null && next.value == next.next.value) {
                    next = next.next;
                }
                // 移除链表上的next节点
                pre.next = next.next;
                next = next.next;
            }else {
                pre = pre.next;
                next = next.next;
            }
        }
        return origin.next;
    }


    class ListNode {
        int value;

        ListNode next;

        public ListNode(int value) {
            this.value = value;
        }

        public ListNode(int value, ListNode next) {
            this.value = value;
            this.next = next;
        }
    }


}
